package huixue.cn.com.rxapp.rx;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Fade;
import android.transition.Slide;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import huixue.cn.com.library.util.LogUtils;
import huixue.cn.com.library.util.ToastUtils;
import huixue.cn.com.rxapp.Main2Activity;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.TestActivity;
import huixue.cn.com.rxapp.base.BaseActivity;
import huixue.cn.com.rxapp.kchart.ui.KChartActivity;
import huixue.cn.com.rxapp.login.ui.LoginActivity;
import huixue.cn.com.rxapp.matrix.MatrixActivity;
import huixue.cn.com.rxapp.rx.adapter.MainAdapter;
import huixue.cn.com.rxapp.rx.entity.Item;
import huixue.cn.com.rxapp.rx.presenter.MainPresenter;
import huixue.cn.com.rxapp.stock.KLine3Activity;
import huixue.cn.com.rxapp.ui.BinderActivity;
import huixue.cn.com.rxapp.ui.CoinActivity;
import huixue.cn.com.rxapp.ui.DownloadActivity;
import huixue.cn.com.rxapp.ui.JNIActivity;
import huixue.cn.com.rxapp.ui.RatioViewActivity;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class RxMainActivity
        extends BaseActivity<MainPresenter,MainPresenter.MainUI>
        implements MainPresenter.MainUI {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    private MainAdapter mMainAdapter;

    private List<Item> mDatas;

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected MainPresenter.MainUI getUI() {
        return this;
    }

    @Override
    public int getLayout() {
        return R.layout.activity_rx_main;
    }

    @Override
    public void initView() {
        mDatas = new ArrayList<>();
        mDatas.add(new Item("contactMap",null));
        mDatas.add(new Item("contactMap",null));
        mDatas.add(new Item("btn_entry_service",StartServiceActivity.class));
        mDatas.add(new Item("btn_recycle_view",RecycleViewTestActivity.class));
        mDatas.add(new Item("btn_hello_chart",HelloChartActivity.class));
        mDatas.add(new Item("btn_scroller",ScrollerActivity.class));
        mDatas.add(new Item("btn_login",LoginActivity.class));
        mDatas.add(new Item("btn_rx_contact",TestActivity.class));
        mDatas.add(new Item("btn_k_line",KLineActivity.class));
        mDatas.add(new Item("btn_k_line2",KLine2Activity.class));
        mDatas.add(new Item("btn_k_line3",KLine3Activity.class));
        mDatas.add(new Item("btn_aidl",Main2Activity.class));
        mDatas.add(new Item("btn_aidl2",BinderActivity.class));
        mDatas.add(new Item("btn_h_recycle",CoinActivity.class));
        mDatas.add(new Item("btn_ratio",RatioViewActivity.class));
        mDatas.add(new Item("自定义宽高比布局",MatrixActivity.class));
        mDatas.add(new Item("btn_jni",JNIActivity.class));
        mDatas.add(new Item("btn_download",DownloadActivity.class));
        mDatas.add(new Item("btn_slide",SlideActivity.class));
        mDatas.add(new Item("btn_test",huixue.cn.com.rxapp.ui.TestActivity.class));
        mDatas.add(new Item("btn_dagger",huixue.cn.com.rxapp.ui.Dagger2Activity.class));
        mDatas.add(new Item("选择男女",huixue.cn.com.rxapp.ui.CheckActivity.class));
        mDatas.add(new Item("btn_bitmap_mesh",huixue.cn.com.rxapp.ui.BitmapActivity.class));
        mDatas.add(new Item("图片的颜色处理",huixue.cn.com.rxapp.ui.BitmapColorActivity.class));
        mDatas.add(new Item("图片的颜色处理2",BitmapColor2Activity.class));
        mDatas.add(new Item("图片的颜色处理3",BitmapColorActivity3.class));
        mDatas.add(new Item("Android动画",AnimatorActivity.class));
        mDatas.add(new Item("Android双缓冲技术",BuffeDisplayActivity.class));
        mDatas.add(new Item("BottomNavigationView",NavigationViewActivity.class));

        mDatas.add(new Item("SurfaceView自定义相机",SurfaceCameraActivity.class));

        mDatas.add(new Item("GLSurfaceView自定义相机",GLSurfaceCameraActivity.class));

        mDatas.add(new Item("Android线程池",ThreadPoolActivity.class));

        mDatas.add(new Item("K线图", KChartActivity.class));

        mDatas.add(new Item("手势密码", LockActivity.class));

        mDatas.add(new Item("ContentProvide", ContentProviderActivity.class));


        mDatas.add(new Item("HookActivity", HookActivity.class));

        mDatas.add(new Item("MessagerActivity", MessagerActivity.class));

        mDatas.add(new Item("AppBarLayout", AppBarLayoutActivity.class));

        mDatas.add(new Item("RxJava2", RxJava2Activity.class));

        mDatas.add(new Item("加载大图", BigImageActivity.class));

        mDatas.add(new Item("材料设计", MaterialDesignActivity.class));
        mDatas.add(new Item("材料设计2", MaterialDesign2Activity.class));
        mDatas.add(new Item("材料设计3", MaterialDesign3Activity.class));

        //new Handler().postDelayed()
    }

    @Override
    public void addEvent() {

        //淡出

        getWindow().setEnterTransition(new Slide());
        getWindow().setExitTransition(new Slide());

        mMainAdapter = new MainAdapter(R.layout.layout_item_main,mDatas);


        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);

        recyclerview.addItemDecoration(new RecycleItemDecoration(10));

        mMainAdapter.setOnItemClickListener(((adapter, view, position) -> {
            Item item = mDatas.get(position);
            if(item.getAction()!=null){
                Intent intent = new Intent(RxMainActivity.this,item.getAction());
                startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
            }else if(item.getName().equals("contactMap")){
                doSomething();
            }
        }));

        recyclerview.setAdapter(mMainAdapter);
    }

    private void doSomething() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> e) throws Exception {
                e.onNext(1);
                e.onNext(2);
                e.onNext(3);
            }
        })
                .concatMap(new Function<Integer, ObservableSource<String>>() {
            @Override
            public ObservableSource<String> apply(Integer integer) throws Exception {
                List<String> list = new ArrayList<>();
                for (int i = 0; i < 3; i++) {
                    list.add("I am value " + integer);
                }
                int delayTime = (int) (1 + Math.random() * 10);
                return Observable.fromIterable(list).delay(delayTime, TimeUnit.MILLISECONDS);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(/*new Consumer<String>() {
                    @Override
                    public void accept(String result) throws Exception {
                        LogUtils.e("=accept=" + result);
                        tvRxResult.append(result + "\n");
                    }
                }*/
                        new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(String s) {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        }
                );



    }


    public void test() throws Exception {
        Observable.create((ObservableOnSubscribe<Integer>) e -> {
            e.onNext(1);
            e.onNext(2);
            e.onComplete();
        }).map(new Function<Integer, Integer>() {
            @Override
            public Integer apply(Integer i) throws Exception {
                return i + 10;
            }
        })
                .doOnNext(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer i) throws Exception {
                        System.out.println("doOnNext : i= " + i);
                    }
                })
                .subscribe(i -> System.out.println("onNext : i= " + i));
    }

}
